package com.scrx.ymg.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.scrx.ymg.entity.AgentSuperMarketSet;
import com.scrx.ymg.entity.HeadEntrepot;
import com.scrx.ymg.entity.SuperMarketEntrepot;
import com.scrx.ymg.entity.SuperMarketInfo;
import com.scrx.ymg.entity.SupermarketSet;
import com.scrx.ymg.service.entrepot.ISuperMarketEntrepotService;
import com.scrx.ymg.service.product.IProductService;
import com.scrx.ymg.service.supermarket.ISuperMarketService;

@Controller
@RequestMapping("/productaAction")
public class ProductaAction {
	 
	 @Autowired
     private IProductService products;
	 
	 @Autowired
     private ISuperMarketEntrepotService smes;
	 
	 @Autowired
	 private ISuperMarketService sms;
	 
	 
	    
	    /*** 
	     * 上传文件 用@RequestParam注解来指定表单上的file为MultipartFile 
	     *  
	     * @param file 
	     * @return 
	     */ 
	    @ResponseBody
	    @RequestMapping(value ="inputProductFileUpload",produces = "text/json;charset=UTF-8")  
	    public String inputProductFileUpload(MultipartFile file,MultipartFile bigFile,HeadEntrepot head,MultipartFile detailFile) {
	        return products.save(head,file,bigFile,detailFile);
	    }
	    
	    /**
	     * 修改总部仓库商品
	     * @param file
	     * @param head
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="updateHeadProduct",produces = "text/json;charset=UTF-8")  
	    public String updateHeadProduct(MultipartFile file,MultipartFile bigFile,HeadEntrepot head,
	    		MultipartFile detailFile) {
	        return products.updateHeadProduct(head,file,bigFile,detailFile);
	    }
	    
	    /**
	     * 查询总部仓库
	     * @param head
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="showProduct",produces = "text/json;charset=UTF-8")
	    public String showProduct(HeadEntrepot head,int pageNum,int pageCount,String superId){
            Map<Object, Object> map= products.selectByCondition(head, pageNum, pageCount);
            List<HeadEntrepot> headlist =(List<HeadEntrepot>)map.get("headlist");
            List<SuperMarketEntrepot> superen=smes.getHotBySuperId(superId, 0);
            for (HeadEntrepot headEntrepot : headlist) {
				for (SuperMarketEntrepot superEntrepot : superen) {
					if(headEntrepot.getBarCode().equals(superEntrepot.getBarCode())){
						headEntrepot.setInactive1(1);
						break;
					}
				}
			}
            map.put("headlist", headlist);
            Gson gson = new Gson();
	    	return gson.toJson(map);
	    }
	    
	    /**
	     * 查询总部仓库
	     * @param head
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="showProducts",produces = "text/json;charset=UTF-8")
	    public String showProducts(HeadEntrepot head,int pageNum,int pageCount){
            Map<Object, Object> map= products.selectByCondition(head, pageNum, pageCount);
            Gson gson = new Gson();
	    	return gson.toJson(map);
	    	
	    }
	    
	    /**
	     * 超市总部仓库上架到超市仓库
	     * @param superEnterpot 上架的商品信息
	     * @param headID  总部商品ID
	     * @return  返回false:失败
	     */
	    @ResponseBody
	    @RequestMapping(value ="putaway",produces = "text/json;charset=UTF-8")
	    public String putaway(SuperMarketEntrepot superEnterpot,int headID){
	    	    String isOK=products.putaway(superEnterpot,headID);
	    	
	    	return isOK;
	    }
	    
	    /**
	     * 查询超市仓库
	     * @param head
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="getProduct",produces = "text/json;charset=UTF-8")
	    public String getProduct(SuperMarketEntrepot superen,int pageNum,int pageCount){
            Map<Object, Object> map= products.selectByCondition(superen, pageNum, pageCount);
            Gson gson = new Gson();
	    	return gson.toJson(map);
	    }
	    
	    /**
	     * 设置超市信息
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="setSupermarketSet",produces = "text/json;charset=UTF-8")
	    public String setSupermarketSet(SupermarketSet set){
	    	set.setId(products.getSupermarketSet(set.getSupermarketId()).getId());
	       return products.updateSupermarketSet(set);
	    }
	    
	    /**
	     * 查询超市开店信息
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="getSupermarketSet",produces = "text/json;charset=UTF-8")
	    public String getSupermarketSet(int superID){
	    	Gson gson = new Gson();
	    	SupermarketSet set=products.getSupermarketSet(superID);
	    	return gson.toJson(set);
	    }
	    
	    /**
	     * 小程序查询超市详细信息
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="getSupermarketSetAndInfo",produces = "text/json;charset=UTF-8")
	    public String getSupermarketSetAndInfo(int superID){
	    	  Map<Object, Object> map =new HashMap<Object, Object>();
	    	Gson gson = new Gson();
	    	SupermarketSet set=products.getSupermarketSet(superID);
	    	SuperMarketInfo info =sms.findByID(superID);
	    	map.put("SupermarketSet", set);
	    	map.put("SuperMarketInfo", info);
	    	return gson.toJson(map);
	    }

	    
	    
	    /**
	     * 根据id查询总部商品
	     * @param supermarket
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="getHeadEntrepot",produces = "text/json;charset=UTF-8")
	    public String getHeadEntrepot(int headId){
	    	Gson gson = new Gson();
	    	HeadEntrepot head=products.updateSuperMarketEntrepot(headId);
	    return gson.toJson(head);
	    }
	    
	    /**
	     * 超市商品更新
	     * @param supermarket
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="updateSuperMarketEntrepot",produces = "text/json;charset=UTF-8")
	    public String updateSuperMarketEntrepot(SuperMarketEntrepot supermarket){
	    return products.updateSuperMarketEntrepot(supermarket);
	    }
	    
	    /**
	     * 超市商品删除
	     * @param supermarket
	     * @return
	     */
	    @ResponseBody
	    @RequestMapping(value ="deleteSuperMarketEntrepot",produces = "text/json;charset=UTF-8")
	    public String deleteSuperMarketEntrepot(SuperMarketEntrepot supermarket){
	    	return products.deleteSuperMarketEntrepot(supermarket);
	    }
	    
}
